﻿
using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common.Helper;
using Besture.UI.Common;
using System.Web.UI;
using Besture.Common;

namespace Besture.UI.Portal.UserCenter
{
    public partial class OrderList : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (StringHelper.Query("delId") !="")
                {
                    GetDelOrder(StringHelper.Query("delId").TrimEnd(','));
                }  
                //订单状态
                var v = QueryDac.GetTable(string.Format("select * from TBDictionaryType where Type=7"));
                ControlHelper.BindDropDownList(ddlOrderStatus1, v, "Name", "Code");
                ControlHelper.BindDropDownList(ddlOrderStatus2, v, "Name", "Code");
                ControlHelper.BindDropDownList(ddlOrderStatus3, v, "Name", "Code");

                BtnSeach2Click(null, null);
                BtnSeach3Click(null, null);
                BtnSeachClick(null,null);

            }
        }

        private int GetDelOrder(string sId)
        {       
           return new OrderDac().DeleteList(sId);
        }

        #region DataSource

        /// <summary>
        /// 全部订单
        /// </summary>
        private DataTable InLoadBind()
        {
            string sWhere = string.Empty;
            DataTable vOrder = new DataTable();
            if ((!string.IsNullOrEmpty(txtOrderName.Value))
                || (!string.IsNullOrEmpty(txtContactName.Value))
                || (!string.IsNullOrEmpty(txtStartCreateTime.Value))
                || (!string.IsNullOrEmpty(txtEndCreateTime.Value)))
            {
                string sOrderName = txtOrderName.Value;
                string sContactName = txtContactName.Value;
                string sStartCreateTime = txtStartCreateTime.Value;
                string sEndCreateTime = txtEndCreateTime.Value;

                if (!string.IsNullOrEmpty(txtOrderName.Value))
                {
                    sWhere += string.Format(" OrderNo like '%{0}%'", sOrderName);
                }
                if (!string.IsNullOrEmpty(txtContactName.Value))
                {
                    sWhere += string.Format(" ContactName like '%{0}%'", sContactName);
                }
                if ((!string.IsNullOrEmpty(txtStartCreateTime.Value)) || (!string.IsNullOrEmpty(txtEndCreateTime.Value)))
                {

                    sWhere += string.Format(" CreateTime >=  '{0}' and CreateTime  <= '{1}'", sStartCreateTime, sEndCreateTime);
                }

            }

            if (Convert.ToInt32(ddlOrderStatus1.SelectedValue) > 0)
            {
                sWhere = string.Format(" OrderStatus ={0} ", ddlOrderStatus1.SelectedValue);
            }
            if (Convert.ToInt32(ddlOrderStatus2.SelectedValue) > 0)
            {
                sWhere = string.Format(" OrderStatus ={0} ", ddlOrderStatus2.SelectedValue);
            }
            if (Convert.ToInt32(ddlOrderStatus3.SelectedValue) > 0)
            {
                sWhere = string.Format(" OrderStatus ={0} ", ddlOrderStatus3.SelectedValue);
            }

            if (new OrderDac().GetDataTable().Select("UserID=" + LoginUserInfo.ID + "").Length > 0)
            {
                var newOrder = new OrderDac().GetDataTable().Select("UserID=" + LoginUserInfo.ID + "").CopyToDataTable();
                if (newOrder.Select(sWhere).Length > 0)
                    vOrder = newOrder.Select(sWhere).CopyToDataTable();
            }
            return vOrder;
        }

        #endregion DataSource

        #region 翻页 tab

        /// <summary>
        /// 翻页 全部订单
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void AspNetPager1PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            InLoadBind();
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;

        }

        #endregion


        /// <summary>
        /// 全部订单 查询功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSeachClick(object sender, EventArgs e)
        {
            hidID.Value = "0";
            var dt = InLoadBind();
            //全部订单                
            AspNetPager1.RecordCount = dt.Rows.Count;
            PagedDataSource pds1 = new PagedDataSource
            {
                AllowPaging = true,
                DataSource = dt.DefaultView,
                PageSize = AspNetPager1.PageSize,
                CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1
            };
            rpOrder.DataSource = pds1;
            rpOrder.DataBind();
             
            InLoadScriptManager();
        }

        /// <summary>
        /// 绑定数据后重新注册一下 清除input 值
        /// </summary>
        private void InLoadScriptManager()
        {
            txtOrderName.Value = "";
            txtContactName.Value = "";
            txtStartCreateTime.Value = "";
            txtEndCreateTime.Value = "";
            //绑定数据后重新注册一下JS事件
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, GetType(), "GetTabInLoad", "GetTabInLoad()", true);
        }

        /// <summary>
        /// 待入住  查询功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSeach2Click(object sender, EventArgs e)
        {
            hidID.Value = "1";
            var dt = InLoadBind();
            lblOrder2.InnerText = "0";
            if (dt != null && dt.Rows.Count > 0)
            {
                var vDr = dt.Select(" CheckDate >=' " + DateTime.Now.AddDays(-3) + "' ");
                if (vDr.Length > 0)
                {
                    lblOrder2.InnerText = vDr.Length.ToString();
                    rpOrder2.DataSource = vDr.CopyToDataTable();
                    rpOrder2.DataBind();
                }
            }
            else
            { 
                rpOrder2.DataSource = new DataTable();
                rpOrder2.DataBind();
            } 
            InLoadScriptManager();
        }

        /// <summary>
        /// 待评价  查询功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSeach3Click(object sender, EventArgs e)
        {
            hidID.Value = "2";
            var dt = InLoadBind();
            lblOrder3.InnerText = "0";
            if (dt != null && dt.Rows.Count > 0)
            {
                var vDr = dt.Select("  CheckDate < ' " + DateTime.Now.AddDays(-3) + "' and DepartureDate < ' " + DateTime.Now + "' ");
                if (vDr.Length > 0)
                {
                    lblOrder3.InnerText = vDr.Length.ToString();
                    rpOrder3.DataSource = vDr.CopyToDataTable();
                    rpOrder3.DataBind();
                }
            }
            else
            {
                rpOrder3.DataSource = new DataTable();
                rpOrder3.DataBind();
            }
            InLoadScriptManager();
        }


        public string GetReviewsReplyByHotelsId(string hotelsId, string sDepartureDate)
        {
            if (Convert.ToDateTime(sDepartureDate) >= DateTime.Now)
            {
                var v = new AppraisalDac().GetDataTableByHotelsAndUserId(Convert.ToInt32(hotelsId),
                    GetUserLoginModel().ID);
                if (v < 1)
                {
                    return string.Format("<a href='HotelCommentList.aspx?hId={0}' >点评酒店</a>", hotelsId);
                }
            }
            return "";
        }
    }
}
